package com.kmxd.ams.infra.aspect;

import cn.hutool.core.util.ObjectUtil;
import com.google.common.collect.Lists;
import com.kmxd.ams.client.license.IConfLicenseSvc;
import com.kmxd.ams.core.common.constant.ErrorCode;
import com.kmxd.ams.core.exception.LicenseException;
import com.kmxd.ams.core.license.verify.config.LicenseVerifyProperties;
import com.kmxd.ams.core.license.verify.model.LicenseVerifyManager;
import com.kmxd.ams.infra.license.entity.ConfLicense;
import com.kmxd.ams.license.core.model.LicenseResult;
import java.util.List;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Service;

/**
 * 授权信息拦截
 *
 * @author ZuoLG
 */
@Aspect
@Service("LicenseAspect")
@Slf4j
public class LicenseAspect {

  /** 放行的ctl */
  private final List<String> packages = Lists.newArrayList();
  /** 当前表字段信息 */
  @Resource private IConfLicenseSvc confLicenseSvc;

  LicenseAspect() {
    packages.add("com.kmxd.ams.controller.system.index.SysIndexCtl");
    packages.add("com.kmxd.ams.controller.system.login.SysUserLoginCtl");
    packages.add("com.kmxd.ams.controller.system.ConfI18nCtl");
    packages.add("com.kmxd.ams.controller.system.ConfI18nDetailCtl");
    packages.add("com.kmxd.ams.controller.system.SysConfigCtl");
    packages.add("com.kmxd.ams.controller.system.SysCommonCtl");
    packages.add("com.kmxd.ams.controller.system.SysDeptCtl");
    packages.add("com.kmxd.ams.controller.system.SysDictCtl");
    packages.add("com.kmxd.ams.controller.system.SysDictItemCtl");
    packages.add("com.kmxd.ams.controller.system.SysDictSetCtl");
    packages.add("com.kmxd.ams.controller.system.SysDictTabsetCtl");
    packages.add("com.kmxd.ams.controller.system.SysJobsCtl");
    packages.add("com.kmxd.ams.controller.system.SysMenuCtl");
    packages.add("com.kmxd.ams.controller.system.SysMessageInfoCtl");
    packages.add("com.kmxd.ams.controller.system.SysMessagesCtl");
    packages.add("com.kmxd.ams.controller.system.SysNoticeCompanyCtl");
    packages.add("com.kmxd.ams.controller.system.SysNoticeCtl");
    packages.add("com.kmxd.ams.controller.system.SysPostCtl");
    packages.add("com.kmxd.ams.controller.system.SysRoleCtl");
    packages.add("com.kmxd.ams.controller.system.SysUserConfigCtl");
    packages.add("com.kmxd.ams.controller.system.SysUserCtl");
    packages.add("com.kmxd.ams.controller.system.SysUserOnlineCtl");
    packages.add("com.kmxd.ams.controller.license.ConfLicenseCtl");
  }

  /** 定义切点Pointcut */
  @Pointcut("execution(* com.kmxd.ams.controller.*.*.*(..))")
  public void excudeService() {}

  /** 方法开始执行 */
  @Before("excudeService()")
  public void doBefore(JoinPoint joinPoint) {
    String packageName = joinPoint.getTarget().getClass().getName();
    // 不是系统部分进行拦截
    if (!packages.contains(packageName)) {
      ConfLicense confLicense = confLicenseSvc.getLicenseInfo();
      if (ObjectUtil.isEmpty(confLicense)) {
        throw LicenseException.instance(ErrorCode.NO_LICENSE, "请先进行授权后再操作！");
      }
      LicenseVerifyProperties properties = new LicenseVerifyProperties();
      properties.setLicenseCode(
          ObjectUtil.isNotEmpty(confLicense) ? confLicense.getRegCode() : null);
      LicenseVerifyManager licenseVerifyManager = new LicenseVerifyManager();
      LicenseResult verifyResult = licenseVerifyManager.verify(properties.getVerifyParam());
      if (!verifyResult.getResult()) {
        throw LicenseException.instance(ErrorCode.NO_LICENSE, verifyResult.getMessage());
      }
    }
  }
}
